﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Booking_Hotel.BLL;
using Booking_Hotel.DBML;
using System.Web.Security;
using Booking_Hotel.DAL;

namespace Booking_Hotel
{
    public partial class supplier_hotel : System.Web.UI.Page
    {
        Supplier_Hotel entities = new Supplier_Hotel();
        ThanhBHotelDataContext tbk = new ThanhBHotelDataContext();
        public int HotelID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["ss_SupplierID"] = 2;//Gan SupplierID cho Dropdownlist de lay DS khach san cua Supplier
                ShowHotel();
            }
        }
        protected void ShowHotel()
        {
            var hotel = from  sh in tbk.Supplier_Hotels
                         where sh.SupplierID == (int)Session["ss_SupplierID"]
                         select new
                         {
                             Supplier_HotelID= sh.Supplier_HotelID,
                             SupplierID= sh.SupplierID,
                             TenKhachSan= sh.TenKhachSan,
                             DiaChiKhachSan= sh.DiaChiKhachSan,
                             TieuChuanSao= sh.TieuChuanSao,
                             Description= sh.Description
                         };
            GridView1.DataSource = hotel;
            GridView1.DataBind();

        }

        protected void Bt_Insert_Click(object sender, EventArgs e)
        {
            try
            {
                entities = new Supplier_Hotel();
                entities.SupplierID = (int)Session["ss_SupplierID"];
                entities.TenKhachSan = txt_TenKhachSan.Text.Trim();
                entities.DiaChiKhachSan = txt_DiaChiKhachSan.Text.Trim() ;
                entities.TieuChuanSao = Byte.Parse(DDL_TieuChuanSao.SelectedValue.ToString());
                entities.Description = txt_Desciption.Text.Trim();
                Supplier_HotelDAO sh = new Supplier_HotelDAO();
                sh.addSupplier_Hotel(entities);
                ShowHotel();

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void Bt_CapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                int check_Supplier_HotelID = (int)(Session["ss_Supplier_HotelID"]);
                if (check_Supplier_HotelID != null)
                {
                    entities = new Supplier_Hotel();
                    entities.Supplier_HotelID = check_Supplier_HotelID;
                    entities.TenKhachSan = txt_TenKhachSan.Text.Trim();
                    entities.DiaChiKhachSan = txt_DiaChiKhachSan.Text.Trim();
                    entities.TieuChuanSao = Byte.Parse(DDL_TieuChuanSao.SelectedValue.ToString());
                    entities.Description = txt_Desciption.Text.Trim();
                    Supplier_HotelDAO sh = new Supplier_HotelDAO();
                    sh.updateSupplier_Hotel(entities);
                    ShowHotel();
                }
                else
                {
                    MyInclude.MessageBox.ShowMessage("Vui lòng chọn đối tượng cần update dưới lưới", this.Page);

                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 0)
            {
                e.Row.Cells[0].Visible = false;
                e.Row.Cells[1].Visible = false;
            }
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridViewRow select = GridView1.Rows[e.NewSelectedIndex];
            Session["ss_Supplier_HotelID"] = int.Parse(select.Cells[0].Text.Trim());
            txt_TenKhachSan.Text = select.Cells[2].Text.Trim();
            txt_DiaChiKhachSan.Text = select.Cells[3].Text.Trim();
            DDL_TieuChuanSao.SelectedValue = select.Cells[4].Text.Trim();
            txt_Desciption.Text = select.Cells[5].Text.Trim();
           
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton bt_Delete = sender as ImageButton;
            GridViewRow row = (GridViewRow)bt_Delete.NamingContainer;
            HotelID = int.Parse(row.Cells[0].Text.Trim());//Supplier_Hotel_DichVuID trên lưới.
            Supplier_Hotel sh = tbk.Supplier_Hotels.Where(c => c.Supplier_HotelID == HotelID).Single();
            tbk.Supplier_Hotels.DeleteOnSubmit(sh);
            tbk.SubmitChanges();
            ShowHotel();
        }

      
    }
}